package com.jiangjiang.eshop.commodity.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiangjiang.eshop.commodity.convert.BrandConvert;
import com.jiangjiang.eshop.commodity.dao.BrandMapper;
import com.jiangjiang.eshop.commodity.domain.BrandEntity;
import com.jiangjiang.eshop.commodity.dto.BrandDTO;
import com.jiangjiang.eshop.commodity.dto.BrandQueryPageReq;
import com.jiangjiang.eshop.common.constant.PathType;
import com.jiangjiang.eshop.common.util.FileUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;

/**
 * @Author Jianghaowen
 * @Date 2021/7/13
 * @Description: TODO
 * @Version V1.0
 **/
@Service
public class BrandService extends ServiceImpl<BrandMapper, BrandEntity> {

    @Autowired
    private BrandConvert brandConvert;
    /**
     * logo图片的路径类型
     */
    @Value("${commodity.brand.image.upload.logo.path.type}")
    private String logoPathType;
    /**
     * logo图片的上传路径
     */
    @Value("${commodity.brand.image.upload.logo.path}")
    private String logoPath;
    /**
     * 品牌授权认证图片的路径类型
     */
    @Value("${commodity.brand.image.upload.authVoucher.path.type}")
    private String authVoucherPathType;
    /**
     * 品牌授权认证图片的上传路径
     */
    @Value("${commodity.brand.image.upload.authVoucher.path}")
    private String authVoucherPath;


    /**
     * 品牌数据分页查询
     *
     * @param req
     * @return
     */
    public Page<BrandDTO> queryForPage(BrandQueryPageReq req) {

        Page<BrandEntity> page = new Page<>(req.getPageNo(), req.getPageNo(), true);
        LambdaQueryWrapper<BrandEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Strings.isNotEmpty(req.getAliasName()), BrandEntity::getAliasName, req.getAliasName())
                .eq(Strings.isNotEmpty(req.getChineseName()), BrandEntity::getChineseName, req.getChineseName())
                .eq(Strings.isNotBlank(req.getEnglishName()), BrandEntity::getEnglishName, req.getEnglishName());

        return brandConvert.convert2DTOaPage(this.getBaseMapper().selectPage(page, wrapper));
    }

    /**
     * 保存品牌数据
     *
     * @param dto             品牌DTO
     * @param logoFile        logo图片
     * @param authVoucherFile 品牌授权认证图片
     * @return 处理结果
     */
    public Boolean saveBrand(BrandDTO dto, MultipartFile logoFile, MultipartFile authVoucherFile) throws Exception {
        dto.setLogoPath(uploadFile(logoFile,logoPathType,logoPath));
        dto.setAuthVoucherPath(uploadFile(authVoucherFile,authVoucherPathType,authVoucherPath));
        return this.save(brandConvert.convert2Entity(dto));
    }

    /**
     * 上传品牌的logo文件
     *
     * @param file 品牌logo文件
     * @param pathType 路径类型
     * @param path 路径
     * @return 处理结果
     */
    private String uploadFile(MultipartFile file, String pathType, String path) throws Exception {
        if (Objects.isNull(file)) { return null;}

        String uploadDirPath = "";
        if (PathType.RELATIVE.equals(pathType)) {
            uploadDirPath = FileUtils.getPathByRelative(path);
            ;
        } else {
            uploadDirPath = path;
        }

        return FileUtils.uploadFile(file, uploadDirPath);
    }

    /**
     * 根据id查询品牌数据
     *
     * @param id 品牌id
     * @return 查询结果
     */
    public BrandDTO queryById(Long id) {
        return brandConvert.convert2DTO(this.getById(id));
    }

    /**
     * 更新品牌
     *
     * @param brand 品牌数据
     * @return 处理结果
     */
    public Boolean updateBrand(BrandDTO brand) {
        return this.updateById(brandConvert.convert2Entity(brand));
    }

    /**
     * 删除品牌数据
     * @param id 品牌id
     * @return 处理结果
     * @throws Exception
     */
    public Boolean removeBrand(Long id) throws Exception {
        BrandDTO brandDTO = this.queryById(id);
        FileUtils.deleteFile(brandDTO.getLogoPath());
        FileUtils.deleteFile(brandDTO.getAuthVoucherPath());
        return this.removeById(id);
    }
}
